String.prototype.getQueryStringValue = function (name) {
  if (!this || !name) return;
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == name) {
      return pair[1];
    }
  }
  return false;
};

Array.prototype.toTree = function () {
  let that = this;
  let map = {},
    node,
    result = [],
    i;
  for (i = 0; i < that.length; i += 1) {
    map[that[i].id] = i;
    // 要讲children初始化为数组
    that[i].children = [];
  }
  for (i = 0; i < that.length; i++) {
    node = that[i];
    if (node.parentId !== null) {
      // 这里通过map来得到父节点的位置
      let parent = that[map[node.parentId]];
      if (parent) that[map[node.parentId]].children.push(node);
      else result.push(node);
    } else {
      result.push(node);
    }
  }
  return result;
};

Array.prototype.remove = function (item) {
  return this.filter((x) => x != item);
};

Number.prototype.toMoney = function () {
  const digitsRE = /(\d{3})(?=\d)/g;
  let value = typeof this === "String" ? parseFloat(value) : this;
  if (!isFinite(value) || (!value && value !== 0)) return "";
  let decimals = 2;
  let stringified = Math.abs(value).toFixed(decimals);
  let _int = decimals ? stringified.slice(0, -1 - decimals) : stringified;
  let i = _int.length % 3;
  let head = i > 0 ? _int.slice(0, i) + (_int.length > 3 ? "," : "") : "";
  let _float = decimals ? stringified.slice(-1 - decimals) : "";
  let sign = value < 0 ? "-" : "";
  return sign + head + _int.slice(i).replace(digitsRE, "$1,") + _float;
};

String.prototype.toMoney = function () {
  if (!this || this == 0) return "";
  let value = parseFloat(this);

  return value.toMoney();
};

Number.prototype.toChineseMoney = function () {
  let n = this;

  if (!n || n == 0) return "";
  if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) {
    return ""; //判断数据是否大于0
  }

  var unit = "千百拾亿千百拾万千百拾元角分",
    str = "";
  n += "00";

  var indexpoint = n.indexOf("."); // 如果是小数，截取小数点前面的位数

  if (indexpoint >= 0) {
    n = n.substring(0, indexpoint) + n.substr(indexpoint + 1, 2); // 若为小数，截取需要使用的unit单位
  }

  unit = unit.substr(unit.length - n.length); // 若为整数，截取需要使用的unit单位
  for (var i = 0; i < n.length; i++) {
    str += "零壹贰叁肆伍陆柒捌玖".charAt(n.charAt(i)) + unit.charAt(i); //遍历转化为大写的数字
  }

  return str
    .replace(/零(千|百|拾|角)/g, "零")
    .replace(/(零)+/g, "零")
    .replace(/零(万|亿|元)/g, "$1")
    .replace(/(亿)万|壹(拾)/g, "$1$2")
    .replace(/^元零?|零分/g, "")
    .replace(/元$/g, "元整"); // 替换掉数字里面的零字符，得到结果
};

Date.prototype.toPeriod = function () {
  let year = this.getFullYear();
  let month = this.getMonth() + 1;

  return year * 100 + month;
};

Date.prototype.nowYearMonth = function () {
  let date = new Date();
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  if (month < 10) month = `0${month}`;

  return `${year}-${month}`;
};

Date.prototype.format = function (format) {
  let date = this;
  const o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
    "H+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
    a: date.getHours() < 12 ? "上午" : "下午", // 上午/下午
    A: date.getHours() < 12 ? "AM" : "PM", // AM/PM
  };
  if (/(y+)/.test(format)) {
    format = format.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return format;
};
